package com.fr.data.core.db.dialect;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.FRCoreContext;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

public class DialectFactory
{
  private static Dialect defaultDialect;
  private static Map dialectMap = new HashMap();

  public static Dialect getDefaultDialect()
  {
    if (defaultDialect == null)
      defaultDialect = new AbstractDialect();
    return defaultDialect;
  }

  public static Dialect generateDialect(Connection paramConnection)
  {
    DatabaseMetaData localDatabaseMetaData;
    try
    {
      localDatabaseMetaData = paramConnection.getMetaData();
      String str = localDatabaseMetaData.getDatabaseProductName();
      int i = -1;
      try
      {
        i = localDatabaseMetaData.getDatabaseMajorVersion();
      }
      catch (Throwable localThrowable)
      {
      }
      if (StringUtils.isBlank(str))
        return getDefaultDialect();
      DBDialectMapper localDBDialectMapper = (DBDialectMapper)dialectMap.get(str);
      if (localDBDialectMapper == null)
        return getDefaultDialect();
      return localDBDialectMapper.getDialect(i);
    }
    catch (SQLException localSQLException)
    {
    }
    return getDefaultDialect();
  }

  static
  {
    dialectMap.put("Oracle", new DBDialectMapper()
    {
      public Dialect getDialect(int paramInt)
      {
        if (paramInt < 9)
          return new Oracle8iDialect();
        return new OracleDialect();
      }
    });
    dialectMap.put("Microsoft SQL Server", new DBDialectMapper()
    {
      public Dialect getDialect(int paramInt)
      {
        switch (paramInt)
        {
        case 8:
          return new MSSQL2000Dialect();
        case 9:
          return new MSSQL2005Dialect();
        }
        return new MSSQLDialect();
      }
    });
    dialectMap.put("Microsoft SQL Server Database", new VersionInsensitiveMapper(MSSQLDialect.class.getName()));
    dialectMap.put("ACCESS", new VersionInsensitiveMapper(AccessDialect.class.getName()));
    dialectMap.put("Sybase SQL Server", new VersionInsensitiveMapper(SybaseDialect.class.getName()));
    dialectMap.put("Adaptive Server Enterprise", new VersionInsensitiveMapper(SybaseDialect.class.getName()));
    dialectMap.put("HSQL Database Engine", new VersionInsensitiveMapper(HSQLDialect.class.getName()));
    dialectMap.put("H2", new VersionInsensitiveMapper(H2Dialect.class.getName()));
    dialectMap.put("MySQL", new VersionInsensitiveMapper(MySQLDialect.class.getName()));
    dialectMap.put("PostgreSQL", new VersionInsensitiveMapper(PostgreSQLDialect.class.getName()));
    dialectMap.put("Apache Derby", new VersionInsensitiveMapper(DerbyDialect.class.getName()));
    dialectMap.put("Informix Dynamic Server", new VersionInsensitiveMapper(InformixDialect.class.getName()));
    dialectMap.put("DB2/NT", new DBDialectMapper()
    {
      public Dialect getDialect(int paramInt)
      {
        switch (paramInt)
        {
        case 6:
          return new DB2V6Dialect();
        case 7:
          return new DB2V7Dialect();
        case 8:
          return new DB2V8Dialect();
        }
        return new DB2Dialect();
      }
    });
    dialectMap.put("DB2/LINUX", new DBDialectMapper()
    {
      public Dialect getDialect(int paramInt)
      {
        switch (paramInt)
        {
        case 6:
          return new DB2V6Dialect();
        case 7:
          return new DB2V7Dialect();
        case 8:
          return new DB2V8Dialect();
        }
        return new DB2Dialect();
      }
    });
    dialectMap.put("DB2/6000", new DBDialectMapper()
    {
      public Dialect getDialect(int paramInt)
      {
        switch (paramInt)
        {
        case 6:
          return new DB2V6Dialect();
        case 7:
          return new DB2V7Dialect();
        case 8:
          return new DB2V8Dialect();
        }
        return new DB2Dialect();
      }
    });
    dialectMap.put("DB2/HPUX", new DBDialectMapper()
    {
      public Dialect getDialect(int paramInt)
      {
        switch (paramInt)
        {
        case 6:
          return new DB2V6Dialect();
        case 7:
          return new DB2V7Dialect();
        case 8:
          return new DB2V8Dialect();
        }
        return new DB2Dialect();
      }
    });
    dialectMap.put("DB2/SUN", new DBDialectMapper()
    {
      public Dialect getDialect(int paramInt)
      {
        switch (paramInt)
        {
        case 6:
          return new DB2V6Dialect();
        case 7:
          return new DB2V7Dialect();
        case 8:
          return new DB2V8Dialect();
        }
        return new DB2Dialect();
      }
    });
    dialectMap.put("DB2/LINUX390", new DBDialectMapper()
    {
      public Dialect getDialect(int paramInt)
      {
        switch (paramInt)
        {
        case 6:
          return new DB2V6Dialect();
        case 7:
          return new DB2V7Dialect();
        case 8:
          return new DB2V8Dialect();
        }
        return new DB2Dialect();
      }
    });
    dialectMap.put("DB2/AIX64", new DBDialectMapper()
    {
      public Dialect getDialect(int paramInt)
      {
        switch (paramInt)
        {
        case 6:
          return new DB2V6Dialect();
        case 7:
          return new DB2V7Dialect();
        case 8:
          return new DB2V8Dialect();
        }
        return new DB2Dialect();
      }
    });
  }

  private static class VersionInsensitiveMapper
    implements DialectFactory.DBDialectMapper
  {
    String dialectClass = null;

    public VersionInsensitiveMapper(String paramString)
    {
      this.dialectClass = paramString;
    }

    public Dialect getDialect(int paramInt)
    {
      try
      {
        return ((Dialect)FRCoreContext.classForName(this.dialectClass).newInstance());
      }
      catch (Exception localException)
      {
        FRContext.getLogger().warning(localException.getMessage());
      }
      return DialectFactory.getDefaultDialect();
    }
  }

  private static abstract interface DBDialectMapper
  {
    public abstract Dialect getDialect(int paramInt);
  }
}